package cn.bone.auth.security.base.component;

import cn.bone.auth.security.base.utils.RSAUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;

@Component
@Data
public class RSAKeyProperties {

    @Value("${security.key.public-key}")
    private String publicKeyStr;

    @Value("${security.key.private-key}")
    private String privateKeyStr;

    private PublicKey publicKey;
    private PrivateKey privateKey;

    @PostConstruct
    public void loadKey(){
        try {
            publicKey = RSAUtil.getPublicKey(publicKeyStr);
            privateKey = RSAUtil.getPrivateKey(privateKeyStr);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeySpecException e) {
            e.printStackTrace();
        }
    }
}
